<?php
/**
 * Created by PhpStorm.
 * User: zeaven
 * Date: 14/12/19
 * Time: 13:26
 */


namespace Addons\CreditScript\Controller;

use Addons\CreditScript\Controller\BaseController;

class MemberCheckinController extends BaseController {

    /**
     * 配置项
     * @return array|void
     */
    function fields() {
        return array (
            array (
                'name' => 'first_week',
                'title' => '第一周奖励:',
                'type' => 'text',
                'remark'=>'此奖励将在原有配置上加成',
                'value' => '1',
                'is_must' => 1,
                'is_show' => 1
            ),
            array (
                'name' => 'second_week',
                'title' => '第二周奖励:',
                'type' => 'text',
                'remark'=>'此奖励将在原有配置上加成',
                'value' => '2',
                'is_must' => 1,
                'is_show' => 1
            ),
            array (
                'name' => 'third_week',
                'title' => '第三周奖励:',
                'type' => 'text',
                'remark'=>'此奖励将在原有配置上加成',
                'value' => '3',
                'is_must' => 1,
                'is_show' => 1
            )
        );
    }

    /**
     * 计算积分
     * 连续登录奖励，第一周连接加1，第二周连续加2，第3周连续加3
     */
    function calc(&$config) {
//        $oneDay = 86400;
//        $model = M( 'member_checkin' );
//
//        $map['uid'] = get_mid();
//        $map['token'] = get_token();
//        $check_data = $model->where($map)->find();
//        //读取签到次数
//        $count = empty($check_data) ? 0: intval($check_data['chk_count']);
//        //上次签到时间
//        $lastDay = empty($check_data) ? 0: intval($check_data['last_day']);
//        //超过一天或连续3周后重新计算
//        if ($count === 21 || time() - $lastDay > $oneDay) {
//            $count = 0;
//        }
//        if ($count === 0) {
//            $addition = 0;
//        } else if ($count < 7) {
//            $addition = intval($this->fieldValue('first_week'));
//        } else if ($count < 14) {
//            $addition = intval($this->fieldValue('second_week'));
//        } else if ($count < 21) {
//            $addition = intval($this->fieldValue('third_week'));
//        }
//        //修改积分
//        $config['score'] = intval($config['score']) + $addition;
//        //更新签到状态信息
//        $check_data['chk_count'] = $count + 1;
//        $check_data['last_day'] = time();
//        if (isset($check_data['uid'])) {
//            $model->save($check_data);
//        } else {
//            $check_data['uid'] = get_mid();
//            $check_data['token'] = get_token();
//            $model->add($check_data);
//        }
    }

}